package servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import pojo.User;
import service.RegisterServiceImpl;
import verify.MyVerify;

public class RegisterServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	private RegisterServiceImpl registerServiceImpl;
	private DateFormat dateFormat;
       
    public RegisterServlet2() {
        super();
        registerServiceImpl = new RegisterServiceImpl();
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    }
	

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		DiskFileItemFactory factory = new DiskFileItemFactory(200*1024,new File("D:/data"));
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setFileSizeMax(250*1024);
		String uname="";
		String sal="";
		String birth="";
		String sex="";
		String avatar="";
		try {
			List<FileItem> items = upload.parseRequest(request);
			for(FileItem item:items){
				if (item.isFormField()) {
					if ("uname".equals(item.getFieldName())) {
						uname = item.getString();
					}
					if ("sal".equals(item.getFieldName())) {
						sal = item.getString();
					}
					if ("birth".equals(item.getFieldName())) {
						birth = item.getString();
					}
					if ("sex".equals(item.getFieldName())) {
						sex = item.getString();
					}
					
				}else{
					System.out.println(item.getName()+"..."+item.getSize());
					InputStream in = item.getInputStream();
					byte[] bytes = new byte[512];
					avatar = this.getServletContext().getRealPath("/WEB-INF/upload/"+item.getName());
					System.out.println(avatar);
					OutputStream out = new FileOutputStream(avatar);
					int len = 0;
					while((len=in.read(bytes))!=-1){
						out.write(bytes, 0, len);
					}
					in.close();
					out.close();
				}
			}
			
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		
		String verify = registerServiceImpl.verify2(uname, sal, birth, sex,avatar);
		if (!verify.equals(MyVerify.SUCCESS)) {
			response.sendRedirect("error?info="+verify);
			return;
		}else{
			int se = Integer.parseInt(sex);
			boolean gender = se==0?false:true;
			try {
				User user = new User(null,uname,Double.parseDouble(sal),dateFormat.parse(birth),gender,avatar);
				System.out.println(new String(uname.getBytes(),"UTF-8"));
				String result = registerServiceImpl.register(user);
				if (result.equals("success")) {
					HttpSession session = request.getSession();
					session.setAttribute("user", user);
					request.getRequestDispatcher("info").forward(request, response);
				}else{
					response.sendRedirect("error?info="+result);
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
			
		
	}
}
